﻿using Microsoft.EntityFrameworkCore;
using Sgr.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sgr.EntityFrameworkCore
{
    public class MySqlDbContextConfigurator : IDbContextConfigurator
    {
        public void Configure(DbContextOptionsBuilder builder, string connectionString)
        {
            builder.UseMySql(connectionString,
                ServerVersion.AutoDetect(connectionString),
                optionsBuilder =>
                {
                    optionsBuilder.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);

                    optionsBuilder
                        .MinBatchSize(1)
                        .MaxBatchSize(1000);

                    //// 连接恢复能力配置
                    //.EnableRetryOnFailure(
                    //    maxRetryCount: 3,                          // 最大重试次数
                    //    maxRetryDelay: TimeSpan.FromSeconds(10),   // 最大重试延迟
                    //    errorNumbersToAdd: null)                   // 需要重试的错误码

                    //// 命令超时配置
                    //.CommandTimeout(30)       // 设置命令超时时间为 30 秒

                    //// 迁移程序集配置
                    //.MigrationsAssembly(typeof(Program).Assembly.FullName) // 指定迁移程序集

                    //// 性能相关配置
                    //.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery) // 使用查询拆分
                    //.UseAutoTransactionBehavior(true)  // 自动事务行为
                });
        }

        public bool IsSupported(DatabaseType dbType)
        {
            return dbType == DatabaseType.MySql;
        }
    }
}